package com.chuangjiangx.member.stored.ddd.domain.subscribe;

import com.chuangjiangx.commons.JacksonUtils;
import com.chuangjiangx.member.stored.ddd.dal.mapper.MbrOrderPayDalMapper;
import com.chuangjiangx.member.basic.ddd.domain.model.MbrUserMapping;
import com.chuangjiangx.member.basic.ddd.domain.model.MbrUserMappingRepository;
import com.chuangjiangx.member.stored.ddd.domain.model.MbrOrder;
import com.chuangjiangx.member.stored.ddd.domain.model.MbrOrderRepository;
import com.chuangjiangx.member.stored.ddd.domain.event.MbrPaySuccessEvent;
import com.chuangjiangx.member.basic.ddd.domain.model.MbrUserMappingType;
import com.chuangjiangx.member.basic.ddd.domain.model.MerchantId;
import com.chuangjiangx.member.stored.ddd.domain.model.MbrOrderType;
import com.chuangjiangx.member.stored.ddd.domain.model.TradeCallBackMbr;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.Objects;

/**
 * @author cj-ch
 * @date 2018/5/23 下午3:54
 * 监听移动支付的支付成功事件
 */
@Slf4j
public class MbrOldOrderPaySuccessListener extends MbrPaySuccessListener{

    @Autowired
    protected MbrOrderPayDalMapper mbrOrderPayDalMapper;
    @Autowired
    private MbrOrderRepository mbrOrderRepository;
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private MbrUserMappingRepository mbrUserMappingRepository;

    @Override
    protected MbrPaySuccessEvent convertMessage(Serializable message) {
        log.info("***接收到移动支付支付成功的订单信息:{}***",message);
        /**
         * 组装 MbrPaySuccessEvent
         */
        TradeCallBackMbr callBackMbr = JacksonUtils.toObject(objectMapper, message.toString(), TradeCallBackMbr.class);
        MbrPaySuccessEvent.MbrPaySuccessEventBuild build = new MbrPaySuccessEvent.MbrPaySuccessEventBuild();
        build
                .setPayTerminal(callBackMbr.getPayTerminal())
                .setPayType(callBackMbr.getPayType())
                .setPayEntry(callBackMbr.getPayEntry())
                .setPayChannelId(callBackMbr.getPayChannel())
                .setOrderPayNumber(callBackMbr.getOrderNumber())
                .setAmount(callBackMbr.getAmount())
                .setRealPayAmount(callBackMbr.getRealPayAmount())
                .setMerchantId(callBackMbr.getMerchantId())
                .setMerchantUserId(callBackMbr.getMerchantUserId())
                .setMemberId(callBackMbr.getMemberId());

        //查询第三方订单编号
        //如果查询到可能是充值时会员这边的预下单
        String outTraceNo = mbrOrderPayDalMapper.selectOutTradeNoWithOrderId(callBackMbr.getOrderId());
        MbrOrder mbrOrder = null;
        if(StringUtils.isNotBlank(outTraceNo)){
            mbrOrder = mbrOrderRepository.fromOrderNumber(outTraceNo);
        }
        if(null == mbrOrder){
            //非会员卡支付 普通消费(不是充值,不是卡券下单后支付)
            //认为是普通的消费

            // 自动识别会员身份
            if(null == callBackMbr.getMemberId()){
                if(Objects.equals(1,callBackMbr.getPayChannel())){
                    //微信官方渠道
                    //获取openid
                    String openid = mbrOrderPayDalMapper.selectWxOpenidWithOrderId(callBackMbr.getOrderId());
                    if(StringUtils.isNotBlank(openid)){
                        // 查询映射
                        MbrUserMapping from = mbrUserMappingRepository.from(openid, new MerchantId(callBackMbr.getMerchantId()), MbrUserMappingType.WX);
                        if(from != null){
                            // 成功识别出会员身份
                            build.setMemberId(from.getMemberId().getId());
                        }
                    }
                }
            }
            build.setServiceType(MbrPaySuccessEvent.Type.CONSUMER);
        } else {
            // 会员这边预下单后发起的支付
            // 1.C扫B充值  2.使用卡券后使用非会员支付
            build.setMbrOrderId(mbrOrder.getId().getId());
            build.setMemberId(mbrOrder.getMemberId().getId());
            if(MbrOrderType.RECHARGE.equals(mbrOrder.getType())){
                build.setServiceType(MbrPaySuccessEvent.Type.RECHARGE);
            }else{
                build.setServiceType(MbrPaySuccessEvent.Type.CONSUMER);
            }
            if(null != mbrOrder.getMbrHasCouponId()){
                //通过卡券下单后支付
                build.setMbrHasCouponId(mbrOrder.getMbrHasCouponId().getId());
            }
        }
        MbrPaySuccessEvent event = build.build();
        return event;
    }
}
